#![allow(non_snake_case)]
use leptos::*;
use server_fn::ServerFnError;
use shq_common::prelude::Deptment;

// 部门信息
#[server(DeptMenus, "/api")]
pub async fn fetch_dept_menus() -> Result<Vec<Deptment>, ServerFnError> {
    use shq_pas_backend::get_deptment;
    let res = get_deptment().await.map_err(|e| {
        ServerFnError::new(format!("获取部门信息失败：{}", e))
    })?;
    Ok(res)
}
// 保存审核专员对应的部门
#[server(SaveDept, "/api")]
pub async fn save_shhr_dept(
    ny: u32,
    wfid: u32,
    emid: u32,
    dpids: Vec<u32>
) -> Result<(), ServerFnError> {
    use shq_pas_backend::create_dept_approvers;
    let res = create_dept_approvers(ny, wfid, emid, dpids).await.map_err(|e| {
        ServerFnError::new(format!("保存审核专员对应的部门失败：{}", e))
    })?;
    Ok(res)
}

// 保存审核专员对应的部门
#[server(DelDept, "/api")]
pub async fn del_shhr_dept(ny: u32, wfid: u32, emid: u32, dpid: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_dept_approvers;
    let res = del_dept_approvers(ny, wfid, emid, dpid).await.map_err(|e| {
        ServerFnError::new(format!("保存审核专员对应的部门失败：{}", e))
    })?;
    Ok(res)
}
// 获取审核专员对应的部门
#[server(GetDept, "/api")]
pub async fn get_shhr_dept(ny: u32, wfid: u32) -> Result<Vec<u32>, ServerFnError> {
    use shq_pas_backend::get_dept_approvers;
    let res = get_dept_approvers(ny, wfid).await.map_err(|e| {
        ServerFnError::new(format!("获取审核专员对应的部门失败：{}", e))
    })?;
    Ok(res)
}
#[server(SaveReportFlow, "/api")]
pub async fn credit_report_flow(ny: u32, emid: u32, emids: Vec<u32>) -> Result<(), ServerFnError> {
    use shq_pas_backend::create_credit_report_flow;
    let res = create_credit_report_flow(ny, emid, emids).await.map_err(|e| {
        ServerFnError::new(format!("创建信贷报告流程失败：{}", e))
    })?;
    Ok(res)
}
#[server(SaveTransactionFlow, "/api")]
pub async fn transaction_flow(
    ny: u32,
    create_emid: u32,
    emids: Vec<u32>
) -> Result<(), ServerFnError> {
    use shq_pas_backend::create_transaction_flow;
    let res = create_transaction_flow(ny, create_emid, emids).await.map_err(|e| {
        ServerFnError::new(format!("创建交易流水流程失败：{}", e))
    })?;
    Ok(res)
}

// 创建家访流程
#[server(SaveHomeVisitFlow, "/api")]
pub async fn home_visit_flow(
    ny: u32,
    create_emid: u32,
    dpids: Vec<u32>
) -> Result<(), ServerFnError> {
    use shq_pas_backend::create_home_visit_flow;
    let res = create_home_visit_flow(ny, create_emid, dpids).await.map_err(|e| {
        ServerFnError::new(format!("创建家访流程失败：{}", e))
    })?;
    Ok(res)
}

// 创建家访流程
#[server(GetHomeVisitFlow, "/api")]
pub async fn get_home_visit_flow(ny: u32,
    create_emid: u32,
    dpids: Vec<u32>,
) -> Result<(), ServerFnError> {
    // use shq_pas_backend::get_id_dept;
    // let dpids = get_id_dept(ny, ids).await.map_err(|e| {
    //     ServerFnError::new(format!("获取部门信息失败：{}", e))
    // })?;
    use shq_pas_backend::create_home_visit_flow;
    let res = create_home_visit_flow(ny, create_emid,dpids).await.map_err(|e| {
        ServerFnError::new(format!("获取流程审核人失败：{}", e))
    })?;
    Ok(res)
}
